package com.baidu.dao.impl;

import com.baidu.common.bean.Goods;
import com.baidu.common.bean.GoodsType;
import com.baidu.common.exception.WebException;
import com.baidu.common.finalword.JdbcConstant;
import com.baidu.dao.GoodsDao;
import com.baidu.dao.abstractclass.BaseAbstract;
import com.baidu.dao.util.JdbcUtil;

import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: BSS
 * @create: 2020/8/20/9:26
 */
public class GoodsDaoImpl extends BaseAbstract<Goods> implements GoodsDao{

    public GoodsDaoImpl() throws WebException {
    }

    @Override
    public Goods get(Integer id) throws WebException {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            preparedStatement = connection.prepareStatement(getPropertiesString(JdbcConstant.GOODS_QUERYBEAN_QUERYBYID));
            preparedStatement.setInt(1,id);
            resultSet = preparedStatement.executeQuery();
            Goods goods = new Goods();
            while (resultSet.next()){
                goods = getGoods(resultSet);
            }
            return goods;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new WebException(e.getMessage());
        }finally {
            closeStatementAndResultSet(preparedStatement,resultSet);
        }
    }

    @Override
    public int update(Goods goods) throws WebException {
        PreparedStatement updateGoodsStatement = null;
        try {
            updateGoodsStatement = connection.prepareStatement(getPropertiesString(JdbcConstant.GOODS_UPDATEBEAN_UPDATEBYID));
            setUpdateSqlParameter(updateGoodsStatement,goods);
            return updateGoodsStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new WebException(e.getMessage());
        }finally {
            JdbcUtil.closeStatement(updateGoodsStatement);
        }
    }

    @Override
    public int delete(Integer id) throws WebException {
        PreparedStatement deleteGoodsStatement = null;
        try {
            deleteGoodsStatement = connection.prepareStatement(getPropertiesString(JdbcConstant.GOODS_DELETEBEAN_DELETEBYID));
            deleteGoodsStatement.setInt(1,id);
            int isSuccess = deleteGoodsStatement.executeUpdate();
            connection.commit();
            return isSuccess;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new WebException(e.getMessage());
        }finally {
            JdbcUtil.closeStatement(deleteGoodsStatement);
        }
    }

    @Override
    public int add(Goods goods) throws WebException {
        PreparedStatement addGoodsStatement = null;
        try {
            addGoodsStatement = connection.prepareStatement(getPropertiesString(JdbcConstant.GOODS_ADDBEAN_ADD));
            setAddSqlParameter(addGoodsStatement,goods);
            int isSuccess = addGoodsStatement.executeUpdate();
            connection.commit();
            return isSuccess;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new WebException(e.getMessage());
        }finally {
            JdbcUtil.closeStatement(addGoodsStatement);
        }
    }

    public void setAddSqlParameter(PreparedStatement statement,Goods goods) throws SQLException {
        int index = 1;
        statement.setString(++index,goods.getName());
        statement.setString(++index,goods.getDescribe());
        statement.setBigDecimal(++index,goods.getPrice());
        statement.setInt(++index,goods.getQuantity());
        statement.setString(++index,goods.getTitle());
        statement.setString(++index,goods.getGoodsType().getId().toString());
        statement.setInt(1,goods.getId());
    }

    public void setUpdateSqlParameter(PreparedStatement statement,Goods goods) throws SQLException {
        int index = 1;
        statement.setString(index,goods.getName());
        statement.setString(++index,goods.getDescribe());
        statement.setBigDecimal(++index,goods.getPrice());
        statement.setInt(++index,goods.getQuantity());
        statement.setString(++index,goods.getTitle());
        statement.setString(++index,goods.getGoodsType().getId().toString());
        statement.setInt(++index,goods.getId());
    }

    @Override
    public List<Goods> queryGoodsTypeList() throws WebException {
        PreparedStatement queryListStatement = null;
        ResultSet resultSet = null;
        try {
            queryListStatement = connection.prepareStatement(getPropertiesString(JdbcConstant.GOODS_SELECT_ALL));
            resultSet = queryListStatement.executeQuery();
            List<Goods> goodsList = new ArrayList<>();
            while (resultSet.next()){
                Goods goods = getGoods(resultSet);
                goodsList.add(goods);
            }
            return goodsList;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new WebException("数据库查询异常");
        }finally {
            closeStatementAndResultSet(queryListStatement,resultSet);
        }
    }

    @Override
    public List<Goods> queryGoodsListByType(Integer id) throws WebException {
        PreparedStatement queryByTidStatement = null;
        ResultSet goodsResult = null;

        try {
            queryByTidStatement = connection.prepareStatement(getPropertiesString(JdbcConstant.GOODS_QUERYBYTID));
            queryByTidStatement.setString(1,id.toString());
            goodsResult = queryByTidStatement.executeQuery();

            List<Goods> goodsList = new ArrayList<>();

            while (goodsResult.next()){
                Goods goods = getGoods(goodsResult);
                goodsList.add(goods);
            }
            return goodsList;
        } catch (WebException | SQLException e) {
            e.printStackTrace();
            throw new WebException(e.getMessage());
        }finally {
            closeStatementAndResultSet(queryByTidStatement,goodsResult);
        }
    }

    public Goods getGoods(ResultSet resultSet) throws WebException {
        PreparedStatement getTypeStatement = null;
        try {
            Goods goods =  new Goods();
            Integer id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            String describe = resultSet.getString("describe");
            BigDecimal price = new BigDecimal(resultSet.getString("price"));
            Integer quantity = Integer.parseInt(resultSet.getString("quantity"));
            String title = resultSet.getString("title");
            Integer typeId = Integer.parseInt(resultSet.getString("tid"));

            getTypeStatement = connection.prepareStatement(getPropertiesString(JdbcConstant.GOODS_QUERYTYPE_QUERYBYID));
            getTypeStatement.setInt(1,typeId);
            ResultSet typeResult = getTypeStatement.executeQuery();
            GoodsType type = new GoodsType();
            if (typeResult.next()){
                String daName = typeResult.getString("name");
                type.setId(typeId);
                type.setName(daName);
            }
            goods.setId(id);
            goods.setName(name);
            goods.setDescribe(describe);
            goods.setPrice(price);
            goods.setQuantity(quantity);
            goods.setTitle(title);
            goods.setGoodsType(type);
            return goods;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new WebException(e.getMessage());
        }finally {
            JdbcUtil.closeStatement(getTypeStatement);
        }
    }
}
